package com.simple.portal.web;

import java.util.List;
import java.util.Timer;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.ContextLoader;

import com.simple.portal.bo.ISendMailBO;
import com.simple.portal.bo.IUserInfoBO;
import com.simple.portal.vo.UserInfoVO;
import com.simple.util.common.Constants;
import com.simple.util.common.ErrorUtil;
import com.simple.util.common.LocaleUtil;
import com.simple.util.common.SimplePropertyLoad;
import com.simple.util.common.SpringUtil;
import com.simple.util.exception.SimpleApplicationException;

@Controller
public class SystemController {
	private static final Logger LOG = Logger
			.getLogger(SystemController.class);
	
	private Timer timer = null;
	
	private static final long TIMER_INTERVAL_PERIOD = 5 * 1000; // every 5 second

	private ISendMailBO sendMailBO;

	private IUserInfoBO userInfoBO;

	@PostConstruct
    public void init() {
    	timer = new Timer(true);  
    	
    	 timer.schedule(new java.util.TimerTask() {
             public void run() {
                 if(null != ContextLoader.getCurrentWebApplicationContext()){
                	sendMailBO = (ISendMailBO) SpringUtil.getBean("portalSendMailBO");
             		userInfoBO = (IUserInfoBO) SpringUtil.getBean("userInfoBO");	
                	 if(null != sendMailBO && null != userInfoBO){
             			final String eamilRegisterPassword = SimplePropertyLoad.getInstance().getString("email.register.password").trim();
             			//获取是否发送标志
             			if("Y".equals(eamilRegisterPassword)){
             				//是 获取密码为空的列表发送邮件
             				final List<UserInfoVO> userInfoVOList = userInfoBO.findNullPasswordList();
             				for(final UserInfoVO userInfoVO : userInfoVOList){
             					try {
									sendMailBO.sendRegisterAckMail(LocaleUtil.WEBSITE_LOCALE_DEFAULT, userInfoVO.getUserId(), Constants.SYSTEM_USER_ID);
								} catch (SimpleApplicationException e) {
									LOG.info(ErrorUtil.generateStackTraceString(e));
								}
             				} 
             				timer.cancel();
             			}
                	 }
                 }
             }
         }, 5 * 1000, TIMER_INTERVAL_PERIOD); // after 5 second,execute the task, every 5 second
		
    }
	
	
}

